Refactor analytics and Statsig integration for improved client handling#2941
Refactor analytics and Statsig integration for improved client handling#2941
Conversation
- Updated the analytics module to lazily create the analytics client only in the browser, preventing unnecessary fetch calls during server-side rendering (SSR). - Enhanced the homepage hero component to support Statsig client initialization and query overrides, ensuring proper handling of hero layout and subtitle based on user experiments. - Introduced a new function to normalize hero subtitles for better consistency in experiment values. - Cleaned up Statsig server-related code by re-exporting necessary functions and types, streamlining the integration process. This refactor aims to improve performance and maintainability while ensuring accurate experiment tracking and user experience.
Greptile SummaryThis PR refactors the Statsig and analytics integration into a cleaner client/server split: analytics is now lazily initialized browser-side only (fixing SSR
Confidence Score: 4/5Safe to merge; no data loss or security issues found, all findings are style/design concerns. All findings are P2: the sentinel collision is theoretically possible but practically negligible, the truncation is intentional, and the onMount vs afterNavigate concern only affects repeated SPA navigations back to /. No P0/P1 issues found. src/routes/(marketing)/(components)/hero.svelte and src/lib/components/homepage-variations/custom-hero.svelte for the initStatsig call placement; src/lib/statsig/experiment-eval.ts for the sentinel approach. Important Files Changed
|
| if (hasHeroExperimentQueryOverrides(page.url.searchParams)) { | ||
| log('URL query overrides (client layout)', { | ||
| ...resolveHeroQueryOverrides(page.url.searchParams, { | ||
| heroLayout, | ||
| heroSubtitle: subtitle, | ||
| heroLayout: clientHeroLayout ?? heroLayout, | ||
| heroSubtitle: clientHeroSubtitle ?? subtitle, | ||
| heroTitle: title |
There was a problem hiding this comment.
initStatsig called from onMount instead of afterNavigate
The initStatsig JSDoc in client.ts explicitly says: "Call from afterNavigate so client-side navigations (e.g. /docs → /) still receive bootstrap." On an SPA navigation back to /, onMount fires again but appliedBootstrapPayload is already set, so the re-init branch is skipped and the fresh bootstrap from the new server load is never applied. The same pattern appears in custom-hero.svelte.
| if (!browser) return null; | ||
| if (!analyticsClient) { | ||
| analyticsClient = Analytics({ | ||
| app: 'appwrite', |
There was a problem hiding this comment.
Unhandled rejection on
.then() with no arguments
getAnalyticsClient()?.track(name, data).then() attaches .then() solely to suppress the floating-promise lint warning, but any rejection from track() becomes an unhandled promise rejection. Prefer void casting instead:
| app: 'appwrite', | |
| void getAnalyticsClient()?.track(name, data); |
This refactor aims to improve performance and maintainability while ensuring accurate experiment tracking and user experience.
What does this PR do?
(Provide a description of what this PR does.)
Test Plan
(Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work.)
Related PRs and Issues
(If this PR is related to any other PR or resolves any issue or related to any issue link all related PR and issues here.)
Have you read the Contributing Guidelines on issues?
(Write your answer here.)